package elitech.vietnam.weatherfashion;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import elitech.vietnam.weatherfashion.backgroundtask.NewPostTask;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.ImageHelper;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.dialog.MyProgressDialog;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import eu.janmuller.android.simplecropimage.CropImage;

public class NewPostActivity extends Activity implements OnClickListener {

	public static final String CAM_PATH = Environment.getExternalStorageDirectory() + "/_camera.jpg";
	public static final int CROP_FROM_CAMERA = 951;
	private EditText textbox_content;
	private ImageView image_post;
	private Button button_save, mBtnBack;
	private TextView mTextHeader;
	private ImageButton mButtonCrop;
	
	private String uri;
//	private int request;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_post);

		Utils.Log(WFApplication.getInstance().todayData == null);
		textbox_content = (EditText) findViewById(R.id.newpost_textbox_content);
		image_post = (ImageView) findViewById(R.id.newpost_image);
		button_save = (Button) findViewById(R.id.newpost_button_save);
		mTextHeader = (TextView) findViewById(R.id.newpost_text_header);
		mButtonCrop = (ImageButton) findViewById(R.id.newpost_button_crop);
		mBtnBack = (Button) findViewById(R.id.newpost_button_back);
		
		mTextHeader.setText(Html.fromHtml(getString(R.string.newstyle)));

		this.uri = getIntent().getStringExtra("image");
//		this.request = getIntent().getIntExtra("request", -1);

		if (this.uri == null || this.uri.equals(""))
			return;
		Utils.Log(uri);
		MyProgressDialog dialog = new MyProgressDialog(this);
		dialog.setCancelable(false);
		dialog.show();
		if (ImageHelper.rotateImage(uri, CAM_PATH))
			uri = CAM_PATH;
		System.gc();
		ImageHelper.displayThumbImage(image_post, uri);
		dialog.dismiss();

		button_save.setOnClickListener(this);
		mButtonCrop.setOnClickListener(this);
		mBtnBack.setOnClickListener(this);
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	@Override
	public void onClick(View arg0) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(textbox_content.getWindowToken(), 0);
		if (arg0.getId() == button_save.getId()) {
			MemberDTO user = GlobalValue.getInstance(getApplicationContext()).readUserInfo();
			String result = user.getId() + "_" + System.currentTimeMillis() + ".jpg";
			new NewPostTask(this).execute(uri, result, textbox_content.getText().toString().trim());
		}
		if (arg0.getId() == mButtonCrop.getId()) {
			Intent intent = new Intent(this, CropImage.class);
			intent.putExtra(CropImage.IMAGE_PATH, uri);
			intent.putExtra(CropImage.SCALE, true);
			intent.putExtra(CropImage.ASPECT_X, 0);
			intent.putExtra(CropImage.ASPECT_Y, 0);
			startActivityForResult(intent, CROP_FROM_CAMERA);
		}
		if (arg0.getId() == R.id.newpost_button_back) 
			onBackPressed();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK && requestCode != CROP_FROM_CAMERA)
			return;
		uri = data.getStringExtra(CropImage.IMAGE_PATH);
		ImageHelper.displayThumbImage(image_post, uri);
	}
	
	@Override
	public void finish() {
		File file = new File(NewPostActivity.CAM_PATH);
		if (file.exists())
			file.delete();
		setResult(RESULT_CANCELED);
		super.finish();
	}
}
